package com.qd.p2p.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ScoreConvertService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.checkrule.ScoreBaseInfoRuleCheck;
import com.qdlc.p2p.dal.common.RuleCheckHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.ScoreConvertModel;

/**
 * 积分兑换
 */
public class ScoreConvertAction extends BaseAction<ScoreConvertModel> implements ModelDriven<ScoreConvertModel> {

	@Resource
	private ScoreConvertService scoreConvertService;
	
	private Map<String, Object> data;
	
	/** 积分基本规则 **/
	private ScoreBaseInfoRuleCheck scoreBaseInfoRuleCheck = RuleCheckHelper.getValue(ScoreBaseInfoRuleCheck.class);
	
	/**
	 * 积分兑换
	 * @throws Exception
	 */
	@Action(value = "/member/score/convert/doConvert", interceptorRefs = {@InterceptorRef("ajaxSafe"), @InterceptorRef("globalStack")})
	public void doConvert() throws IOException{
		model.setUser(getSessionUser());
		model.setUserId(getSessionUserId());
		model.setUserName(getSessionUserName());
		scoreConvertService.doConvert(model);
		printWebSuccess();
	}
	
	/**
	 * 获取个人积分兑换记录
	 * 该信息显示在个人中心-积分记录-兑换详情
	 * @throws IOException 
	 */
	@Action(value = "/member/score/convert/list", interceptorRefs = {@InterceptorRef("ajaxSafe"), @InterceptorRef("globalStack")})
	public void list() throws IOException {
		model.setUser(getSessionUser());
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(99);
		}
		PageDataList<ScoreConvertModel> page = scoreConvertService.findPageList(model);
		data = new HashMap<String, Object>();
		data.put("data", page);
		printWebJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 获取最新积分兑换记录
	 * 该信息显示在积分商城首页 “兑换记录”
	 */
	@Action(value = "/member/score/convert/newList")
	public void newList() throws Exception {
		List<ScoreConvertModel> modelList = scoreConvertService.list(scoreBaseInfoRuleCheck.show_convert_num);
		data = new HashMap<String, Object>();
		data.put("data", modelList);
		printWebJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 删除积分兑换
	 * @throws IOException
	 */
	@Action(value = "/member/score/convert/deleteScoreConvert")
	public void deleteScoreConvert() throws IOException {
		scoreConvertService.delete(model.getId());
		printWebSuccess();
	}
	
}
